package com.caration.shell.config;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.content.SharedPreferences;

import com.caration.shell.ShellWallpaperManager;
import com.caration.shell.cr3d.SharedData;
import com.caration.shell.preference.SettingFolderPreference;
import com.caration.shell.theme.inside.ThemeConfiguration;
import com.caration.shell.theme.inside.ThemeManager;

public final class CommonConfig {

	public static final String SP_NAME = "config";

	public static final String KEY_TRANSTION = "transition";
	public static final String KEY_TRANSTION3D = "transition3d";
	public static final String KEY_APP_SORT = "app_sort";
	public static final String KEY_MENU_ORDER = "menu_order";
	public static final String KEY_MENU_INDEX = "menu_index";
	public static final String KEY_MENU_POSTION = "menu_position";
	public static final String KEY_SETTING_MENU = "setting_menu";
	public static final String KEY_PREF_DIRECTION = "preference_direction";
	public static final String KEY_SCREEN_ORIENT = "screen_orientation";
	public static final String KEY_WIDGET_ANIM = "widget_animation";
	public static final String KEY_ICON_EFFECT = "icone_ffect";
	public static final String KEY_SLIDING_SENSI = "sliding_sensitivity";
	public static final String KEY_INDICATOR_MODE = "indicator_mode";
	public static final String KEY_VIBRATION_FEEDBACK = "vibration_feedback";
	public static final String KEY_WALLPAPER_MODE = "wallpaper_mode";
	public static final String KEY_DRAWER_ROWS = "drawer_rows";
	public static final String KEY_DRAWER_COLUMNS = "drawer_columns";
	public static final String KEY_DRAWER_AUTOSIZE = "drawer_autosize";
	public static final String KEY_SCREEN_INFINITE_SCROLL = "screen_infinite_scroll";
	public static final String KEY_DESKTOP_LOCK = "desktop_lock";
	public static final String KEY_DESKTOP_RELOCK = "desktop_relock";
	public static final String KEY_SLIDING_DOCK = "sliding_dock";
	public static final String KEY_AUTO_HIDE_SIDE_BAR = "auto_hide_side_bar";
	public static final String KEY_WALLPAPER_TRANS_SPEED = "wallpaper_transition_speed";
	public static final String KEY_FOLDER_ANIM = "preference_folder_animation";
	public static final String KEY_FOLDER_AUTO_CLOSE = "preference_folder_auto_close";
	public static final String KEY_SCREEN_SCALE = "screen_scale";
	public static final String KEY_THEME_PACKAGE = "theme_package";
	public static final String KEY_THEME_RESETWALLPAPER = "theme_resetwallpaper";
	public static final String KEY_THEME_ICON = "theme_icon";
	public static final String KEY_HIDDEN_APP = "hidden_app";
	public static final String KEY_TEXT_STYLE = "text_style";
	public static final String KEY_VERSION = "version";
	public static final String KEY_SHOW_NOTIFICATION = "show_notification_bar";
	public static final String KEY_PAGE_MENU_BUTTON_ALPHA = "page_menu_button_alpha";
	public static final String KEY_STANDARD_SLIDING_MODE = "standard_sliding_mode";
	public static final String KEY_FAST_SLIDING_MODE = "fast_sliding_mode";
	public static final String KEY_HOME_SCREEN = "home_screen";
	public static final String KEY_PAGE_MENU_BUTTON_STATE = "page_menu_button_state";
	public static final String KEY_SCREEN = "screen";

	private static boolean mIndicatorMode = true;
	private static boolean mVibrationFeedback = true;
	private static boolean mScreenInfiniteScroll = true;
	private static boolean mDesktopLock = false;
	private static boolean mDesktopRelock = true;
	private static boolean mSlidingDock = true;
	private static boolean mAutoHideSideBar = false;
	private static int mSlidingSensitivity = 1;
	private static int mScreen = -2;
	private static int mWallpaperMode;
	private static int mDrawerRows;
	private static int mDrawerColumns;
	private static boolean mDrawerAutoSize;
	private static boolean mIsLiveWallpaper;
	private static int mFolderAnimation;
	private static boolean mFolderAutoClose;
	private static SharedPreferences mSharedPreferences;

	public static int[] getTransArray() {
		int[] array = new int[2];
		SharedPreferences sp = getSharedPreference();
		int transition = sp.getInt(KEY_TRANSTION, 1);
		int transition3d = sp.getInt(KEY_TRANSTION3D, 0);
		array[0] = transition;
		array[1] = transition3d;
		return array;
	}

	public static int getAppSort() {
		return getSharedPreference().getInt(KEY_APP_SORT, 0);
	}

	// public static j C() {
	// SharedPreferences sp = getSharedPreference();
	// return new j(sp.getString(KEY_MENU_ORDER, "0,1,2,3"),
	// sp.getInt(KEY_MENU_INDEX,
	// 1), sp.getInt(
	// KEY_MENU_POSTION, 0));
	// }

	public static String getSettingMenu() {
		return getSharedPreference().getString(KEY_SETTING_MENU, "0,1,2,3,4,5");
	}

	public static int getFolderAnimation() {
		return mFolderAnimation;
	}

	public static boolean isFolderAutoClose() {
		return mFolderAutoClose;
	}

	public static int getPrefDirection() {
		int direct = 0;
		int value = getSharedPreference().getInt(KEY_PREF_DIRECTION, 0);
		if (value >= 0)
			if (value <= 1)
				direct = value;
			else
				direct = 1;
		return direct;
	}

	public static int getScreenOrientation() {
		int orientation = 0;
		int value = getSharedPreference().getInt(KEY_SCREEN_ORIENT, 0);
		if (value >= 0)
			if (value <= 2)
				orientation = value;
			else
				orientation = 2;
		return orientation;
	}

	public static boolean isWidgetAnimationOn() {
		return getSharedPreference().getBoolean(KEY_WIDGET_ANIM, false);
	}

	public static int getIconEffect() {
		return getSharedPreference().getInt(KEY_ICON_EFFECT, 0);
	}

	public static SharedPreferences.Editor getEditor() {
		return getSharedPreference().edit();
	}

	public static SharedPreferences getSharedPreference() {
		if (mSharedPreferences == null)
			mSharedPreferences = SharedData.getContext()
					.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		return mSharedPreferences;
	}

	public static void clear() {
		SharedPreferences.Editor editor = getEditor();
		editor.clear();
		editor.commit();
	}

	// a()
	public static void initialize() {
		int value = getSharedPreference().getInt(KEY_SLIDING_SENSI, 1);
		mSlidingSensitivity = value;
		if (value >= 0) {
			if (mSlidingSensitivity > 4)
				mSlidingSensitivity = 4;
		} else
			mSlidingSensitivity = 0;
		mIndicatorMode = getSharedPreference().getBoolean(KEY_INDICATOR_MODE,
				true);
		mVibrationFeedback = getSharedPreference().getBoolean(
				KEY_VIBRATION_FEEDBACK, true);
		value = getSharedPreference().getInt(KEY_WALLPAPER_MODE,
				ShellWallpaperManager.SCROLL_MODE);
		mWallpaperMode = value;
		if (value != ShellWallpaperManager.SCROLL_MODE)
			mWallpaperMode = ShellWallpaperManager.CENTER_MODE;
		mDrawerRows = getSharedPreference().getInt(KEY_DRAWER_ROWS, 0);
		mDrawerColumns = getSharedPreference().getInt(KEY_DRAWER_COLUMNS, 0);
		mDrawerAutoSize = getSharedPreference().getBoolean(KEY_DRAWER_AUTOSIZE,
				true);
		// TODO:set backgroud alpha of 3d sence
		// e.a(getSharedPreference().getInt("drawer_background_alpha", 70));
		mScreenInfiniteScroll = getSharedPreference().getBoolean(
				KEY_SCREEN_INFINITE_SCROLL, false);
		boolean isLock = getSharedPreference().getBoolean(KEY_DESKTOP_LOCK,
				false);
		mDesktopLock = isLock;
		// com.tsf.shell.workspace3D.x.a = isLock;
		mDesktopRelock = getSharedPreference().getBoolean(KEY_DESKTOP_RELOCK,
				true);
		mSlidingDock = getSharedPreference().getBoolean(KEY_SLIDING_DOCK, true);
		mAutoHideSideBar = getSharedPreference().getBoolean(
				KEY_AUTO_HIDE_SIDE_BAR, false);
		ShellWallpaperManager.setSpeed(getSharedPreference().getInt(
				KEY_WALLPAPER_TRANS_SPEED, 1));
		mFolderAnimation = SettingFolderPreference
				.validate(getSharedPreference().getInt(KEY_FOLDER_ANIM, 0));
		mFolderAutoClose = getSharedPreference().getBoolean(
				KEY_FOLDER_AUTO_CLOSE, false);
	}

	public static void setScreenScale(float scale) {
		SharedPreferences.Editor editor = getEditor();
		editor.putFloat(KEY_SCREEN_SCALE, scale);
		editor.commit();
	}

	public static void setWallpaperSpeed(int speed) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_WALLPAPER_TRANS_SPEED, speed);
		editor.commit();
	}

	public static void setDrawerCellCount(int rows, int columns) {
		SharedPreferences.Editor editor = getEditor();
		mDrawerRows = rows;
		mDrawerColumns = columns;
		editor.putInt(KEY_DRAWER_ROWS, rows);
		editor.putInt(KEY_DRAWER_COLUMNS, columns);
		editor.commit();
	}

	// TODO:
	// public static void a(k paramk) {
	// n localn = new n(paramk);
	// a.a().d(localn);
	// }

	public static void setThemeConfig(ThemeConfiguration paramThemeConfiguration) {
		SharedPreferences.Editor editor = getEditor();
		editor.putString(KEY_THEME_PACKAGE, paramThemeConfiguration.theme);
		editor.putBoolean(KEY_THEME_RESETWALLPAPER,
				paramThemeConfiguration.resetwallpaper);
		editor.putString(KEY_THEME_ICON, "icon:"
				+ paramThemeConfiguration.iconPackage + "/"
				+ paramThemeConfiguration.iconFile + ",mask:"
				+ paramThemeConfiguration.maskPackage + "/"
				+ paramThemeConfiguration.maskFile);
		editor.commit();
	}

	public static void setDesktopLock(boolean isLock) {
		mDesktopLock = isLock;
		// TODO:
		// com.tsf.shell.workspace3D.x.a = isLock;
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_DESKTOP_LOCK, isLock);
		editor.commit();
	}

	public static void setSettingMenu(String settingMenu) {
		SharedPreferences.Editor editor = getEditor();
		editor.putString(KEY_SETTING_MENU, settingMenu);
		editor.commit();
	}

	public static void setMenuStatus(String order, int index, int position) {
		SharedPreferences.Editor editor = getEditor();
		editor.putString(KEY_MENU_ORDER, order);
		editor.putInt(KEY_MENU_INDEX, index);
		editor.putInt(KEY_MENU_POSTION, position);
		editor.commit();
	}

	public static void setHiddenApps(ArrayList appNames) {
		Iterator iterator = appNames.iterator();
		String allAppsName;
		String appName;
		for (allAppsName = ""; iterator.hasNext(); allAppsName = allAppsName
				+ appName + ",")
			appName = (String) iterator.next();
		if (allAppsName.length() > 0)
			allAppsName = allAppsName.substring(0, -1 + allAppsName.length());
		SharedPreferences.Editor editor = getEditor();
		editor.putString(KEY_HIDDEN_APP, allAppsName);
		editor.commit();
	}

	public static void setDesktopRelock(boolean relock) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_DESKTOP_RELOCK, relock);
		editor.commit();
		mDesktopRelock = relock;
	}

	public static void setTextStyle(int textStyle) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_TEXT_STYLE, textStyle);
		editor.commit();
	}

	public static void setTransition(int transition, int transtion3d) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_TRANSTION, transition);
		editor.putInt(KEY_TRANSTION3D, transtion3d);
		editor.commit();
	}

	public static void getThemeConfig(ThemeConfiguration paramThemeConfiguration) {
		paramThemeConfiguration.theme = getSharedPreference().getString(
				KEY_THEME_PACKAGE, "");
		paramThemeConfiguration.resetwallpaper = getSharedPreference()
				.getBoolean(KEY_THEME_RESETWALLPAPER, true);
		ThemeManager.parseIconConfig(paramThemeConfiguration,
				getSharedPreference().getString(KEY_THEME_ICON, ""));
	}

	public static void setAutoHideSideBar(boolean isAuto) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_AUTO_HIDE_SIDE_BAR, isAuto);
		editor.commit();
	}

	public static void setResetThemeWallpaper(boolean resetWallpaper) {
		SharedPreferences.Editor editor = getEditor();
		editor.putString(KEY_THEME_PACKAGE, "");
		editor.putBoolean(KEY_THEME_RESETWALLPAPER, resetWallpaper);
		editor.putString(KEY_THEME_ICON, "icon:");
		editor.commit();
	}

	public static boolean isDesktopLock() {
		return mDesktopLock;
	}

	public static void setVersion(int version) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_VERSION, version);
		editor.commit();
	}

	public static void setSlidingDock(boolean isSlidingDock) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_SLIDING_DOCK, isSlidingDock);
		editor.commit();
	}

	public static void setDrawerAutoSize(boolean isDrawerAutoSize) {
		mDrawerAutoSize = isDrawerAutoSize;
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_DRAWER_AUTOSIZE, isDrawerAutoSize);
		editor.commit();
	}

	public static boolean isDesktopRelock() {
		return mDesktopRelock;
	}

	public static void setBackgourdAlpha(int alpha) {
		// TODO:
		// SharedPreferences.Editor editor = getEditor();
		// e.a(alpha);
		// editor.putInt("drawer_background_alpha", e.a());
		// editor.commit();
	}

	public static void setInfiniteScroll(boolean isInfiniteScroll) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_SCREEN_INFINITE_SCROLL, isInfiniteScroll);
		editor.commit();
	}

	public static void setVibrationFeedback(boolean isVibrationFeedback) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_VIBRATION_FEEDBACK, isVibrationFeedback);
		editor.commit();
		mVibrationFeedback = isVibrationFeedback;
	}

	public static boolean isAutoHideSideBar() {
		return mAutoHideSideBar;
	}

	public static int getTextStyle() {
		int textStyle = getSharedPreference().getInt(KEY_TEXT_STYLE, 0);
		return textStyle;
	}

	public static void setWallpaperMode(int mode) {
		mWallpaperMode = mode;
		if (mode != ShellWallpaperManager.SCROLL_MODE)
			mWallpaperMode = ShellWallpaperManager.CENTER_MODE;
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_WALLPAPER_MODE, mode);
		editor.commit();
	}

	public static void setNotificationStatus(boolean notify) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_SHOW_NOTIFICATION, notify);
		editor.commit();
	}

	public static void setIndicatorMode(boolean indicatorMode) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_INDICATOR_MODE, indicatorMode);
		editor.commit();
		mIndicatorMode = indicatorMode;
	}

	public static void setSlidingSensitivity(int sensitivity) {
		if (sensitivity >= 0) {
			if (sensitivity > 4)
				sensitivity = 4;
		} else
			sensitivity = 0;
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_SLIDING_SENSI, sensitivity);
		editor.commit();
		mSlidingSensitivity = sensitivity;
	}

	public static void setLiveWallpaperFlag(boolean isLiveWallpaper) {
		mIsLiveWallpaper = isLiveWallpaper;
	}

	public static boolean isSlidingDock() {
		return mSlidingDock;
	}

	public static void setPageMenuButtonAlpha(int alpha) {
		if (alpha >= 0) {
			if (alpha > 100)
				alpha = 100;
		} else
			alpha = 0;
		// fm.h().e().a(alpha);TODO:
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_PAGE_MENU_BUTTON_ALPHA, alpha);
		editor.commit();
	}

	public static void setStandardSlidingMode(boolean isStandard) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_STANDARD_SLIDING_MODE, isStandard);
		editor.commit();
	}

	public static boolean isScreenInfiniteScroll() {
		return mScreenInfiniteScroll;
	}

	public static int getVersion() {
		return getSharedPreference().getInt(KEY_VERSION, 1);
	}

	public static void h(int paramInt) {
		// m localm = new m(paramInt);
		// a.a().d(localm);TODO:
	}

	public static void setFastSlidingMode(boolean isFast) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_FAST_SLIDING_MODE, isFast);
		editor.commit();
	}

	public static void setHomeScreen(int index) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_HOME_SCREEN, index);
		editor.commit();
	}

	public static void setPageMenuButtonState(boolean is) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_PAGE_MENU_BUTTON_STATE, is);
		editor.commit();
	}

	public static boolean isShowNotification() {
		return getSharedPreference().getBoolean(KEY_SHOW_NOTIFICATION, true);
	}

	public static int getDrawerRows() {
		return mDrawerRows;
	}

	public static void setAppSort(int type) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_APP_SORT, type);
		editor.commit();
	}

	public static void setFolderAutoClose(boolean autoClose) {
		mFolderAutoClose = autoClose;
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_FOLDER_AUTO_CLOSE, autoClose);
		editor.commit();
	}

	public static int getDrawerColumns() {
		return mDrawerColumns;
	}

	public static void setFolderAnimation(int animType) {
		mFolderAnimation = SettingFolderPreference.validate(animType);
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_FOLDER_ANIM, mFolderAnimation);
		editor.commit();
	}

	public static void setWidgetAnim(boolean paramBoolean) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean(KEY_WIDGET_ANIM, paramBoolean);
		editor.commit();
	}

	public static void setPrefDirection(int paramInt) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_PREF_DIRECTION, paramInt);
		editor.commit();
	}

	public static boolean isDrawerAutoSize() {
		return mDrawerAutoSize;
	}

	public static int getWallpaperMode() {
		return mWallpaperMode;
	}

	public static void setScreenOrient(int orientation) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_SCREEN_ORIENT, orientation);
		editor.commit();
	}

	public static ArrayList<String> getHiddenApps() {
		ArrayList<String> allApps = new ArrayList<String>();
		String str = getSharedPreference().getString(KEY_HIDDEN_APP, "");
		if (str.length() != 0) {
			String[] namesArray = str.split(",");
			final int length = namesArray.length;
			for (int i = 0; i < length; i++)
				allApps.add(namesArray[i]);
		}
		return allApps;
	}

	public static void setIconEffect(int effect) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(KEY_ICON_EFFECT, effect);
		editor.commit();
	}

	public static int getSlidingSensitivity() {
		return mSlidingSensitivity;
	}

	public static boolean isVibrationFeedbackOn() {
		return mVibrationFeedback;
	}

	public static boolean isIndicatorModeOn() {
		return mIndicatorMode;
	}

	public static boolean isLiveWallpaper() {
		return mIsLiveWallpaper;
	}

	public static boolean isStandardSlidingMode() {
		return getSharedPreference()
				.getBoolean(KEY_STANDARD_SLIDING_MODE, true);
	}

	public static boolean isFastSlidingMode() {
		return getSharedPreference().getBoolean(KEY_FAST_SLIDING_MODE, true);
	}

	public static boolean isPageMenuButtonState() {
		return getSharedPreference().getBoolean(KEY_PAGE_MENU_BUTTON_STATE,
				true);
	}

	public static int getPageMenuButtonAlpha() {
		int alpha = 100;
		int value = getSharedPreference().getInt(KEY_PAGE_MENU_BUTTON_ALPHA,
				alpha);
		if (value >= 0) {
			if (value <= alpha)
				alpha = value;
		} else
			alpha = 0;
		return alpha;
	}

	public static float getScreenScale() {
		float scale = 1.0F;
		float value = getSharedPreference().getFloat(KEY_SCREEN_SCALE, 0.0F);
		if (value != 0.0F) {
			// if (value <= f.a)
			if (value < scale)
				return scale;
		} else {
			scale = value;
			return scale;
		}
		// scale = f.a;
		return scale;
	}

	public static int getScreen() {
		int screen = -1;
		if (mScreen == -2) {
			int value = getSharedPreference().getInt(KEY_SCREEN, screen);
			// int i2 = z().a;
			if (value >= screen)
				screen = value;
			// if (screen >= i2)
			// screen = i2 - 1; TODO:
			mScreen = screen;
		} else {
			screen = mScreen;
		}
		return screen;
	}

	public static int getHomeScreen() {
		int homeScreen = -1;
		// int i3 = z().a;
		int value = getSharedPreference().getInt(KEY_HOME_SCREEN, homeScreen);
		if (value >= homeScreen)
			homeScreen = value;
		// if (homeScreen >= i3)
		// homeScreen = i3 - 1;
		return homeScreen;
	}

	public static void clearErrorRecord() {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean("startupError", false);
		editor.putInt("startupErrorTimes", 0);
		editor.commit();
	}

	public static void setStartupError(boolean isError) {
		SharedPreferences.Editor editor = getEditor();
		editor.putBoolean("startupError", isError);
		editor.commit();
	}

	public static boolean isStartupError() {
		return getSharedPreference().getBoolean("startupError", false);
	}

	public static void setStartupErrorTimes(int times) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt("startupErrorTimes", times);
		editor.commit();
	}

	public static int getStartupErrorTimes() {
		return getSharedPreference().getInt("startupErrorTimes", 0);
	}

	// TODO:
	// public static k z() {
	// Object localObject = a.e().getContentResolver().query(cg.b, null, null,
	// null, null);
	// ((Cursor) localObject).moveToNext();
	// try {
	// i1 = ((Cursor) localObject)
	// .getInt(((Cursor) localObject).getColumnIndex("extrascreen"));
	// str = ((Cursor) localObject).getString(((Cursor) localObject)
	// .getColumnIndex("screenorder"));
	// i1 = i1;
	// str = str;
	// if (i1 > 12) {
	// i1 = 12;
	// ((Cursor) localObject).close();
	// localObject = new k();
	// ((k) localObject).a = i1;
	// ((k) localObject).b = str;
	// return localObject;
	// }
	// } catch (Exception localException) {
	// while (true) {
	// String str = "";
	// int i1 = 3;
	// continue;
	// if (i1 >= 3)
	// continue;
	// i1 = 3;
	// }
	// }
	// }
}
